package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    private ExtCproductService extCproductService;

    @Reference
    private FactoryService factoryService;

    @RequestMapping("/list")
    public String list(String contractId,String contractProductId, @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {

        //1. 查询当前附件下的附件数据,得到pageInfo

        ExtCproductExample extCproductExample = new ExtCproductExample();

        //由于我们只需要查询当前附件的附件

        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);

        PageInfo<ExtCproduct> pageInfo = extCproductService.findByPage(extCproductExample, pageNum, pageSize);

        request.setAttribute("pageInfo", pageInfo);

        //2. 查询生成附件的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        //3. 把附件id存储到域中


        request.setAttribute("contractId", contractId);
        request.setAttribute("contractProductId", contractProductId);

        return "cargo/extc/extc-list";


    }

      /*
    作用：添加附件，更新附件
    url:  /extCproduct/edit.do
    参数：ExtCproduct 附件对象信息
    返回值 : 附件列表页面
 */

    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct ,MultipartFile productPhoto) {
        //附件的创建人
        extCproduct.setCreateBy(getLoginUser().getId());

        //附件的创建人所属的部门

        extCproduct.setCreateDept(getLoginUser().getDeptId());

        //附件的创建人所属的企业id

        extCproduct.setCompanyName(getLoginUserCompanyId());

        //附件的创建人所属的企业名称

        extCproduct.setCompanyName(getLoginUserCompanyName());


    /*    if(productPhoto!=null){
            //存储七牛中
            String url = fileUploadUtil.upload(productPhoto); //返回图片上传成功的url
            //把url路径存储到对象
            extCproduct.setProductImage("http://"+url);
        }*/



        //判断一个变量是否为空串,相当于上面的语句
        if (StringUtils.isEmpty(extCproduct.getId())){
            //添加
            extCproductService.save(extCproduct);
        }else {
            //更新
            extCproductService.update(extCproduct);
        }

        return  "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();

    }

        /*
         作用 ： 进入更新附件的页面
         url : /cargo/extCproduct/toUpdate.do?id=093226d4-24a2-4deb-90d5-5a2fadea10a9
        参数 : 附件id
        返回值 :product-update
     */


    @RequestMapping("/toUpdate")
    public String toUpdate(String id,String contractId,String contractProductId) {
        
        //1. 根据附件的id查找附件
        ExtCproduct extCproduct = extCproductService.findById(id);

        //2. 存储到request域中
        request.setAttribute("extCproduct", extCproduct);


        //3. 生成附件的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        //4. 合同的id

        request.setAttribute("contractId",contractId);

        //5. 货物的id

        request.setAttribute("contractProductId",contractProductId);


        //6. 请求转发到extc-update页面
        return "cargo/extc/extc-update";

    }


    @RequestMapping("/delete")
    public String delete(String id, String contractId,String contractProductId) {
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId=" + contractId+"&contractProductId="+contractProductId;
    }


    /*
        作用 ：进入上传附件的页面
        url :/cargo/extCproduct/toImport.do?contractId=902567ff-1ba5-4258-bccb-9e18586569e4
       参数 : 附件的id
       返回值 : 进入上传附件的页面
    */

    @RequestMapping("/toImport")
    public String toImport(String contractId) {
        request.setAttribute("contractId", contractId);
        return "cargo/product/product-import";
    }


}




















